package com.dog.modules.upms.web;

import cn.hutool.core.bean.BeanUtil;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.core.utils.JsonUtils;
import com.dog.framework.mybatis.utils.PageUtils;
import com.dog.modules.upms.domain.entity.SysUser;
import com.dog.modules.upms.dto.request.SysUserPageQueryDto;
import com.dog.modules.upms.dto.request.UserReqDto;
import com.dog.modules.upms.dto.response.SysUserRespVO;
import com.dog.modules.upms.dto.response.UserRespDto;
import com.dog.modules.upms.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @SneakyThrows
    @PostMapping("/add")
    public UserRespDto add(@RequestBody UserReqDto userReqDto) {
        System.out.println("===============hello world===========");
        log.info("添加用户请求参数 {}", JsonUtils.toJsonString(userReqDto));
        //Thread.sleep(300000);
        UserRespDto userRespDto = new UserRespDto();
        BeanUtils.copyProperties(userReqDto, userRespDto);
        userRespDto.setMessage("success");
        return userRespDto;
    }


    @PostMapping("/list")
    public List<SysUserRespVO> list(@RequestBody UserReqDto userReqDto) {
        log.info("添加用户请求参数 {}", JsonUtils.toJsonString(userReqDto));
        List<SysUser> sysUserList = sysUserService.list();
        SysUserRespVO respVO =  new SysUserRespVO();
        List<SysUserRespVO> respVOList = BeanUtil.copyToList(sysUserList,SysUserRespVO.class);

        log.info("返回参数 ：{}",JsonUtils.toJsonString(respVOList));
        return respVOList;
    }



    @PostMapping("/page")
    public PageInfo<SysUserRespVO> page(@RequestBody SysUserPageQueryDto userReqDto) {
        PageInfo<SysUserRespVO> respVOPageInfo = new PageInfo<>();
        log.info("添加用户请求参数 {}", JsonUtils.toJsonString(userReqDto));
        PageHelper.startPage(userReqDto.getPageNum(),userReqDto.getPageSize());
        List<SysUser> sysUserList = sysUserService.list();
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUserList);
        BeanUtil.copyProperties(pageInfo,respVOPageInfo);
        List<SysUserRespVO> respVOList = BeanUtil.copyToList(pageInfo.getList(),SysUserRespVO.class);
        respVOPageInfo.setList(respVOList);
        log.info("返回参数 ：{}",JsonUtils.toJsonString(respVOPageInfo));
        return respVOPageInfo;
    }



    @PostMapping("/page2")
    public PageResult<SysUserRespVO> page2(@RequestBody SysUserPageQueryDto reqDto) {
        PageResult<SysUserRespVO> data = new PageResult<>();
        log.info("添加用户请求参数 {}", JsonUtils.toJsonString(reqDto));
        PageResult<SysUser> sysUserPageResult = PageUtils.getPage(reqDto, d -> sysUserService.list());
        List<SysUserRespVO> respVOList = BeanUtil.copyToList(sysUserPageResult.getRows(),SysUserRespVO.class);
        BeanUtil.copyProperties(sysUserPageResult,data);
        data.setRows(respVOList);
        log.info("返回参数 ：{}",JsonUtils.toJsonString(data));
        return data;
    }




}
